﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CoilIntelStorage.Controller;
using CoilIntelStorage.Models;
using CoilIntelStorage.Utils;
using CoilIntelStorage.ViewModels;

namespace CoilIntelStorage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = System.Windows.WindowState.Maximized;
            this.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            //初始化库位集合
            InitPositionElements();
            InitStockUi();
            this.DataContext = new MainWindowViewModel(this);
        }

        private void dg_goods_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Id" ||
                e.Column.Header.ToString() == "Type" ||
                   e.Column.Header.ToString() == "Start_point" ||
                      e.Column.Header.ToString() == "End_point" ||
                      e.Column.Header.ToString() == "Build_time" ||
                      e.Column.Header.ToString() == "Start_time" ||
                      e.Column.Header.ToString() == "End_time" ||
                      e.Column.Header.ToString() == "Status" ||
                      e.Column.Header.ToString() == "Goods_code" ||
                      e.Column.Header.ToString() == "TypeStr" ||
                      e.Column.Header.ToString() == "OrderNo" ||
                      e.Column.Header.ToString() == "LabelText"
                )
            {
                e.Cancel = true;
            }
        }

        private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "Id" ||
                e.Column.Header.ToString() == "Start_point" ||
                   e.Column.Header.ToString() == "End_point" ||
                      e.Column.Header.ToString() == "Send_time" ||
                      e.Column.Header.ToString() == "Socket_code" ||
                      e.Column.Header.ToString() == "Content" ||
                      e.Column.Header.ToString() == "Task_id" ||
                      e.Column.Header.ToString() == "OrderNo"
                )
            {
                e.Cancel = true;
            }
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.Items.Count > 0)
            {
                var border = VisualTreeHelper.GetChild(dataGrid, 0) as Decorator;
                if (border != null)
                {
                    var scroll = border.Child as ScrollViewer;
                    if (scroll != null) scroll.ScrollToEnd();
                }
            }
        }

        /// <summary>
        /// 是否拖动
        /// </summary>
        private bool isDragging;
        private int draggingStatus = 0;
        /// <summary>
        /// 点击位置
        /// </summary>
        private Point clickPosition;
        /// <summary>
        /// 拖动对象原始位置
        /// </summary>
        private Point originalPosition;
        /// <summary>
        /// 库位集合
        /// </summary>
        private List<UIElement> positionElements;

        /// <summary>
        /// 鼠标左键点击按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void position_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            draggingStatus = 1;
            isDragging = true;
            var draggableElement = sender as UIElement;
            clickPosition = e.GetPosition(this);
            draggableElement.CaptureMouse();
            Window window = Window.GetWindow(draggableElement);
            Border draggableBorder = draggableElement as Border;
            if (originalPosition == null)
            {
                originalPosition = draggableElement.TransformToAncestor(window).Transform(new Point(0, 0));
            }
            draggableBorder.BorderBrush = Brushes.Black;
            Thickness thickness = new Thickness
            {
                Top = 1d,
                Bottom = 1d,
                Left = 1d,
                Right = 1d
            };
            Thickness thickness2 = new Thickness
            {
                Top = 8d,
                Bottom = 8d,
                Left = 8d,
                Right = 8d
            };
            draggableBorder.BorderThickness = thickness;
            draggableBorder.Margin = thickness2;
            draggableBorder.Background = Brushes.Gray;
            Image child = FindVisualChild<Image>(draggableBorder);
            child.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/LuJuan.png"));
            Panel.SetZIndex(draggableBorder, 100);
        }

        /// <summary>
        /// 鼠标左键点击后抬起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void position_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;
            draggingStatus = 2;
            var draggableElement = sender as UIElement;
            draggableElement.ReleaseMouseCapture();
            var transform = draggableElement.RenderTransform as TranslateTransform;
            if (transform == null)
            {
                transform = new TranslateTransform();
                draggableElement.RenderTransform = transform;
            }
            //当拖动后返回原来的坐标
            transform.X = originalPosition.X;
            transform.Y = originalPosition.Y;
            Border draggableBorder = draggableElement as Border;
            draggableBorder.BorderBrush = Brushes.Transparent;
            draggableBorder.Opacity = 1d;
            Thickness thickness = new Thickness
            {
                Top = 0d,
                Bottom = 0d,
                Left = 0d,
                Right = 0d
            };
            draggableBorder.BorderThickness = thickness;
            Thickness thickness2 = new Thickness
            {
                Top = 5d,
                Bottom = 5d,
                Left = 5d,
                Right = 5d
            };
            draggableBorder.Margin = thickness2;
            var bc = new BrushConverter();
            draggableBorder.Background = (Brush)bc.ConvertFrom("#EDE5E3");
            Panel.SetZIndex(draggableBorder, -1);
            Image child = FindVisualChild<Image>(draggableBorder);
            child.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/DiZuo.png"));
        }

        /// <summary>
        /// 鼠标拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void position_MouseMove(object sender, MouseEventArgs e)
        {
            var draggableElement = sender as UIElement;
            if (isDragging && draggableElement != null)
            {
                Point currentPosition = e.GetPosition(this.Parent as UIElement);
                var transform = draggableElement.RenderTransform as TranslateTransform;
                if (transform == null)
                {
                    transform = new TranslateTransform();
                    draggableElement.RenderTransform = transform;
                }
                transform.X = currentPosition.X - clickPosition.X;
                transform.Y = currentPosition.Y - clickPosition.Y;
                //ChangeColor(transform.X, transform.Y);
                //draggableElement.IsHitTestVisible = false;
                //遍历所有控件 判断鼠标坐标是否在该控件坐标区域内，如果是则把对应的控件样式修改成叠加的样式
                //for (int i = 0; i < positionElements.Count; i++)
                //{
                //    UIElement positionElement = positionElements[i];
                //    Border positionBorder = positionElement as Border;
                //    System.Windows.Point pp = Mouse.GetPosition(positionElement as FrameworkElement);//WPF方法
                //    System.Windows.Point ppp = (e.Source as FrameworkElement).PointToScreen(pp);
                //    txt_displayInfo.Text += "控件Name:" + positionBorder.Name + "\r\nX:" + ppp.X + ",Y:" + ppp.Y;
                //}
            }
        }
        /// <summary>
        /// 判断该坐标下是否有其他控件
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        //private void ChangeColor(double x, double y)
        //{
        //    //遍历初始化过来的区位控件集合判断移动到的位置是否有其他控件
        //    foreach (UIElement positionElement in positionElements)
        //    {
        //        Border positionBorder = positionElement as Border;
        //        Vector vector = VisualTreeHelper.GetOffset(positionBorder);
        //        //Point controlPoint = positionElement.TransformToAncestor(window).Transform(new Point(0, 0));
        //        //Point controlPoint = positionBorder.PointToScreen(new Point(0d, 0d));
        //        Point controlPoint = new Point(vector.X, vector.Y);
        //        //计算出该控件所占用坐标区域
        //        //double positionX = positionBorder.ActualWidth + controlPoint.X;
        //        //double positionY = positionBorder.ActualHeight + controlPoint.Y;
        //        //如果X轴坐标在该控件内，并且Y轴坐标在该控件内，则修改该控件的样式
        //        //if (x >= controlPoint.X - 100 && x <= controlPoint.X - 70 && y >= controlPoint.Y - 50 && y <= controlPoint.Y )
        //        //{
        //        //    positionBorder.Background = Brushes.Yellow;
        //        //    lbl_displayInfo.Content = "控件Name:" + positionBorder.Name + "\r\n坐标X：" + x + ",Y:" + y;
        //        //}
        //        //else
        //        //{
        //        //    var bc = new BrushConverter();
        //        //    positionBorder.Background = (Brush)bc.ConvertFrom("#EDE5E3");
        //        //}

        //    }
        //}
        /// <summary>
        /// 初始化库位集合
        /// </summary>
        private void InitPositionElements()
        {
            positionElements = new List<UIElement>();
            #region 第1行
            positionElements.Add(this.position_1_1);
            positionElements.Add(this.position_1_2);
            positionElements.Add(this.position_1_3);
            positionElements.Add(this.position_1_4);
            positionElements.Add(this.position_1_5);
            positionElements.Add(this.position_1_6);
            positionElements.Add(this.position_1_7);
            positionElements.Add(this.position_1_8);
            #endregion 第一行
            #region 第二行
            positionElements.Add(this.position_2_1);
            positionElements.Add(this.position_2_2);
            positionElements.Add(this.position_2_3);
            positionElements.Add(this.position_2_4);
            positionElements.Add(this.position_2_5);
            positionElements.Add(this.position_2_6);
            positionElements.Add(this.position_2_7);
            positionElements.Add(this.position_2_8);
            #endregion 第二行
            #region 第三行
            positionElements.Add(this.position_3_3);
            positionElements.Add(this.position_3_4);
            positionElements.Add(this.position_3_5);
            positionElements.Add(this.position_3_6);
            positionElements.Add(this.position_3_7);
            positionElements.Add(this.position_3_8);
            #endregion 第三行
            #region 第四行
            positionElements.Add(this.position_4_3);
            positionElements.Add(this.position_4_4);
            positionElements.Add(this.position_4_5);
            positionElements.Add(this.position_4_6);
            positionElements.Add(this.position_4_7);
            positionElements.Add(this.position_4_8);
            #endregion 第四行
            #region 第五行
            positionElements.Add(this.position_5_1);
            positionElements.Add(this.position_5_2);
            positionElements.Add(this.position_5_3);
            positionElements.Add(this.position_5_4);
            positionElements.Add(this.position_5_5);
            positionElements.Add(this.position_5_6);
            positionElements.Add(this.position_5_7);
            positionElements.Add(this.position_5_8);
            positionElements.Add(this.position_5_9);
            positionElements.Add(this.position_5_10);
            positionElements.Add(this.position_5_11);
            positionElements.Add(this.position_5_12);
            positionElements.Add(this.position_5_13);
            #endregion 第五行
            #region 第六行
            positionElements.Add(this.position_6_1);
            positionElements.Add(this.position_6_2);
            positionElements.Add(this.position_6_3);
            positionElements.Add(this.position_6_4);
            positionElements.Add(this.position_6_5);
            positionElements.Add(this.position_6_6);
            positionElements.Add(this.position_6_7);
            positionElements.Add(this.position_6_8);
            positionElements.Add(this.position_6_9);
            positionElements.Add(this.position_6_10);
            positionElements.Add(this.position_6_11);
            positionElements.Add(this.position_6_12);
            positionElements.Add(this.position_6_13);
            #endregion 第六行
            #region 第七行
            positionElements.Add(this.position_7_1);
            positionElements.Add(this.position_7_2);
            positionElements.Add(this.position_7_3);
            positionElements.Add(this.position_7_4);
            positionElements.Add(this.position_7_5);
            positionElements.Add(this.position_7_6);
            positionElements.Add(this.position_7_7);
            positionElements.Add(this.position_7_8);
            positionElements.Add(this.position_7_9);
            positionElements.Add(this.position_7_10);
            positionElements.Add(this.position_7_11);
            positionElements.Add(this.position_7_12);
            positionElements.Add(this.position_7_13);
            #endregion 第七行
            #region 第八行
            positionElements.Add(this.position_8_1);
            positionElements.Add(this.position_8_2);
            positionElements.Add(this.position_8_3);
            positionElements.Add(this.position_8_4);
            positionElements.Add(this.position_8_5);
            positionElements.Add(this.position_8_6);
            positionElements.Add(this.position_8_7);
            positionElements.Add(this.position_8_8);
            positionElements.Add(this.position_8_9);
            positionElements.Add(this.position_8_10);
            positionElements.Add(this.position_8_11);
            positionElements.Add(this.position_8_12);
            positionElements.Add(this.position_8_13);
            #endregion 第八行
            #region 第九行
            positionElements.Add(this.position_9_1);
            positionElements.Add(this.position_9_2);
            positionElements.Add(this.position_9_3);
            positionElements.Add(this.position_9_4);
            positionElements.Add(this.position_9_5);
            positionElements.Add(this.position_9_6);
            positionElements.Add(this.position_9_7);
            positionElements.Add(this.position_9_8);
            positionElements.Add(this.position_9_9);
            positionElements.Add(this.position_9_10);
            positionElements.Add(this.position_9_11);
            positionElements.Add(this.position_9_12);
            positionElements.Add(this.position_9_13);
            #endregion 第九行
            #region 第十行
            positionElements.Add(this.position_10_1);
            positionElements.Add(this.position_10_2);
            positionElements.Add(this.position_10_3);
            positionElements.Add(this.position_10_4);
            positionElements.Add(this.position_10_5);
            positionElements.Add(this.position_10_6);
            positionElements.Add(this.position_10_7);
            positionElements.Add(this.position_10_8);
            positionElements.Add(this.position_10_9);
            positionElements.Add(this.position_10_10);
            positionElements.Add(this.position_10_11);
            positionElements.Add(this.position_10_12);
            positionElements.Add(this.position_10_13);
            #endregion 第十行
            #region 第十一行
            positionElements.Add(this.position_11_1);
            positionElements.Add(this.position_11_2);
            positionElements.Add(this.position_11_3);
            positionElements.Add(this.position_11_4);
            positionElements.Add(this.position_11_5);
            positionElements.Add(this.position_11_6);
            positionElements.Add(this.position_11_7);
            positionElements.Add(this.position_11_8);
            positionElements.Add(this.position_11_9);
            positionElements.Add(this.position_11_10);
            positionElements.Add(this.position_11_11);
            positionElements.Add(this.position_11_12);
            positionElements.Add(this.position_11_13);
            #endregion 第十一行
            #region 第十二行
            positionElements.Add(this.position_12_1);
            positionElements.Add(this.position_12_2);
            positionElements.Add(this.position_12_3);
            positionElements.Add(this.position_12_4);
            positionElements.Add(this.position_12_5);
            positionElements.Add(this.position_12_6);
            positionElements.Add(this.position_12_7);
            positionElements.Add(this.position_12_8);
            positionElements.Add(this.position_12_9);
            positionElements.Add(this.position_12_10);
            positionElements.Add(this.position_12_11);
            positionElements.Add(this.position_12_12);
            #endregion 第十二行
            #region 第十三行
            positionElements.Add(this.position_13_1);
            positionElements.Add(this.position_13_2);
            positionElements.Add(this.position_13_3);
            positionElements.Add(this.position_13_4);
            positionElements.Add(this.position_13_5);
            positionElements.Add(this.position_13_6);
            positionElements.Add(this.position_13_7);
            positionElements.Add(this.position_13_8);
            positionElements.Add(this.position_13_9);
            positionElements.Add(this.position_13_10);
            positionElements.Add(this.position_13_11);
            #endregion 第十三行
            //循环注册鼠标移入移除事件
            foreach (UIElement positionElement in positionElements)
            {
                positionElement.MouseEnter += PositionElement_MouseEnter;
                positionElement.MouseLeave += PositionElement_MouseLeave;
                positionElement.MouseMove += PositionElement_MouseMove;
                Border border = positionElement as Border;
                Thickness thickness = new Thickness
                {
                    Top = 5d,
                    Bottom = 5d,
                    Left = 5d,
                    Right = 5d
                };
                border.Margin = thickness;
                this.grid.RegisterName(border.Name, border);
            }
        }

        /// <summary>
        /// 鼠标移入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PositionElement_MouseEnter(object sender, MouseEventArgs e)
        {
  
            Border positionBorder = sender as Border;
            //MessageBox.Show( e.GetPosition(positionBorder).X.ToString());
            positionBorder.Background = Brushes.Yellow;

            if (draggingStatus == 2)
            {
                draggingStatus = 0;
                Image child = FindVisualChild<Image>(positionBorder);
                child.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/YouLu.png"));
            }
        }

        private void PositionElement_MouseMove(object sender, MouseEventArgs e)
        {
            System.Windows.Point pp = Mouse.GetPosition(sender as FrameworkElement);//WPF方法
            System.Windows.Point ppp = (e.Source as FrameworkElement).PointToScreen(pp);
            Border border = sender as Border;
            //鼠标响应panel左上1/4区域
            if (e.GetPosition(null).X > 0 && e.GetPosition(null).X < (border.RenderSize.Width) && e.GetPosition(null).Y > 0 && e.GetPosition(null).Y < (border.RenderSize.Height))
            {
                txt_displayInfo.Text = "Capture mouse " + border.Name;//Label标签（要想用，别忘了先添加一个），测试用。
            }
            else
            {
                txt_displayInfo.Text = "Can't capture mouse " + border.Name;
            }
        }


        /// <summary>
        /// 鼠标移出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PositionElement_MouseLeave(object sender, MouseEventArgs e)
        {
            Border positionBorder = sender as Border;
            var bc = new BrushConverter();
            positionBorder.Background = (Brush)bc.ConvertFrom("#EDE5E3");
        }

        public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据操作更改库位状态
        /// </summary>
        /// <param name="oper">操作：包含 入库、出库</param>
        /// <param name="position"></param>
        public void ChangePositionStatusByOper(String oper, String startPosition, String endPosition)
        {
            var bc = new BrushConverter();
            if (oper.Equals("开始入库"))
            {
                if (startPosition.Equals("1#"))
                {
                    //针对多线程调用相同对象的控件而使用的方法
                    this.position_in_13_13.Dispatcher.Invoke(
                        new Action(
                             delegate
                             {
                                 this.position_in_13_13.Background = Brushes.Orange;
                             }
                        )
                    );
                    this.position_in_13_12.Dispatcher.Invoke(
                        new Action(
                            delegate
                            {
                                this.position_in_13_12.Background = Brushes.Orange;
                            }
                        )
                    );
                    this.position_in_12_13.Dispatcher.Invoke(
                        new Action(
                            delegate
                            {
                                this.position_in_12_13.Background = Brushes.Orange;
                            }
                        )
                    );
                }
                else if (startPosition.Equals("2#"))
                {
                    this.position_in_4_1.Dispatcher.Invoke(
                      new Action(
                          delegate
                          {
                              this.position_in_4_1.Background = Brushes.Orange;
                          }
                        )
                    );
                }
                grid.Dispatcher.Invoke(
                      new Action(
                          delegate
                          {
                              //position_1_1
                              Border borderEndPosition = grid.FindName("position_" + endPosition) as Border;
                              borderEndPosition.Background = Brushes.Orange;
                          }
                        )
                    );
            }

            else if (oper.Equals("入库结束"))
            {
                if (startPosition.Equals("1#"))
                {
                    this.position_in_13_13.Dispatcher.Invoke(
                        new Action(
                                delegate
                                {
                                    this.position_in_13_13.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                                }
                            )
                    );
                    this.position_in_13_12.Dispatcher.Invoke(
                        new Action(
                                delegate
                                {
                                    this.position_in_13_12.Background = (Brush)bc.ConvertFrom("#EDE5E3");

                                }
                            )
                    );

                    this.position_in_12_13.Dispatcher.Invoke(
                        new Action(
                                delegate
                                {
                                    this.position_in_12_13.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                                }
                            )
                    );
                }
                else if (startPosition.Equals("2#"))
                {
                    this.position_in_4_1.Dispatcher.Invoke(
                        new Action(
                                delegate
                                {
                                    this.position_in_4_1.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                                }
                            )
                    );
                }
                grid.Dispatcher.Invoke(
                     new Action(
                             delegate
                             {
                                 Border borderEndPosition = grid.FindName("position_" + endPosition) as Border;
                                 borderEndPosition.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                                 Image child = FindVisualChild<Image>(borderEndPosition);
                                 child.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/YouLu.png"));
                             }
                         )
                    );

            }
            else if (oper.Equals("开始出库"))
            {
                if (endPosition.Equals("1#"))
                {
                    this.position_in_13_13.Dispatcher.Invoke(
                        new Action(
                                delegate
                                {
                                    this.position_in_13_13.Background = Brushes.Orange;
                                }
                         )
                    );
                    this.position_in_13_12.Dispatcher.Invoke(
                          new Action(
                                delegate
                                {
                                    this.position_in_13_12.Background = Brushes.Orange;
                                }
                          )
                    );
                    this.position_in_12_13.Dispatcher.Invoke(
                        new Action(
                              delegate
                              {
                                  this.position_in_12_13.Background = Brushes.Orange;
                              }
                          )
                    );
                }
                else if (endPosition.Equals("2#"))
                {
                    this.position_in_4_1.Dispatcher.Invoke(
                        new Action(
                              delegate
                              {
                                  this.position_in_4_1.Background = Brushes.Orange;
                              }
                          )
                    );
                }
                grid.Dispatcher.Invoke(
                     new Action(
                             delegate
                             {
                                 Border borderStartPosition = grid.FindName("position_" + startPosition) as Border;
                                 borderStartPosition.Background = Brushes.Orange;
                             }
                          )
                    );
            }
            else if (oper.Equals("出库结束"))
            {
                if (endPosition.Equals("1#"))
                {
                    this.position_in_13_13.Dispatcher.Invoke(
                        new Action(
                            delegate
                            {
                                this.position_in_13_13.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                            }
                          )
                    );
                    this.position_in_13_12.Dispatcher.Invoke(
                        new Action(
                           delegate
                           {
                               this.position_in_13_12.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                           }
                          )
                    );
                    this.position_in_12_13.Dispatcher.Invoke(
                        new Action(
                           delegate
                           {
                               this.position_in_12_13.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                           }
                          )
                    );
                }
                else if (endPosition.Equals("2#"))
                {
                    this.position_in_4_1.Dispatcher.Invoke(
                        new Action(
                           delegate
                           {
                               this.position_in_4_1.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                           }
                          )
                    );
                }
                this.grid.Dispatcher.Invoke(
                       new Action(
                              delegate
                              {
                                  Border borderStartPosition = grid.FindName("position_" + startPosition) as Border;
                                  borderStartPosition.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                              }
                          )
                    );
            }
            else if (oper.Equals("开始挪库"))
            {
                this.grid.Dispatcher.Invoke(
                       new Action(
                              delegate
                              {
                                  Border borderStartPosition = grid.FindName("position_" + startPosition) as Border;
                                  borderStartPosition.Background = Brushes.Orange;
                                  Border borderEndPosition = grid.FindName("position_" + endPosition) as Border;
                                  borderEndPosition.Background = Brushes.Orange;
                              }
                          )
                    );
            }
            else if (oper.Equals("挪库结束"))
            {
                this.grid.Dispatcher.Invoke(
                      new Action(
                             delegate
                             {
                                 Border borderStartPosition = grid.FindName("position_" + startPosition) as Border;
                                 borderStartPosition.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                                 Border borderEndPosition = grid.FindName("position_" + endPosition) as Border;
                                 borderEndPosition.Background = (Brush)bc.ConvertFrom("#EDE5E3");
                             }
                          )
                    );
            }

        }
        /// <summary>
        /// 当该窗体关闭后，主程序以及所有后台线程全部关闭。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWin_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 初始化库存界面UI
        /// </summary>
        private void InitStockUi() {
            DataTable dataTable = DbHelper.GetTable("SELECT * FROM stock WHERE status = 1");
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow dataRow = dataTable.Rows[i];
                string name = dataRow["name"].ToString();
                Border borderEndPosition = grid.FindName("position_" + name) as Border;
                Image child = FindVisualChild<Image>(borderEndPosition);
                child.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/YouLu.png"));
            }
        }
    }
}
